//
//  IBTLoadingView.m
//  Cruiser
//
//  Created by 陈俊俊 on 15/7/11.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "DearLoadingView.h"
#import "DearCommon.h"
@implementation DearLoadingView

#pragma mark - Class Methods
+ (void)showTips:(id)tips {
    if ([tips isKindOfClass:[NSError class]]) {
        [[self class] showHUDMessage:((NSError *)tips).localizedDescription];
    }
    else if ([tips isKindOfClass:[NSString class]]) {
        [[self class] showHUDMessage:tips];
    }
}

+ (void)showHUDMessage:(NSString *)message {
    [DearCommon runOnMainThreadWithoutDeadlocking:^{
        [[self class] showTextOnly:message];
    }];
    
}

+ (DearLoadingView *)showHUDWithText:(NSString *)text
                             inView:(UIView *)view {
    DearLoadingView *hud = [DearLoadingView showHUDAddedTo:view animated:YES];
    if ([text length] < 20) {
        hud.labelText = text;
    }
    else {
        hud.detailsLabelText = text;
    }
    
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

+ (void)showTextOnly:(NSString *)text inView:(UIView *)view {
    DearLoadingView *hud = [[self class] showHUDWithText:text inView:view];
    hud.mode = MBProgressHUDModeText;
    hud.margin = 10.0f;
    //	hud.yOffset = 0.f;
    [hud hide:YES afterDelay:1];
}

+ (void)showTextOnly:(NSString *)text {
    DearLoadingView *hud = [[self class] showHUDWithText:text inView:[[self class] hudShowWindow]];
    hud.mode = MBProgressHUDModeText;
//    hud.margin = 10.0f;
    hud.yOffset = 60.0f;
    [hud hide:YES afterDelay:2];
}

+ (void)showProgressLabel:(NSString *)text  {
    DearLoadingView *hud = [[self class] showHUDWithText:text inView:[[self class] hudShowWindow]];
    hud.mode = MBProgressHUDModeIndeterminate;
}




+ (void)showProgressLabel:(NSString *)text
                   inView:(UIView *)view {
    DearLoadingView *hud = [[self class] showHUDWithText:text inView:view];
    hud.mode = MBProgressHUDModeIndeterminate;
}
+ (void)hideHUD {
    [[self class] hideHUDForView:[[self class] hudShowWindow]];
}
+ (void)hideHUDForView:(UIView *)view{
    [DearLoadingView hideHUDForView:view animated:YES];
}


+ (void)showCustomView:(UIView *)customview
                inView:(UIView *)view {
    DearLoadingView *hud = [[DearLoadingView alloc] initWithView:view];
    hud.customView = customview;
    
    hud.mode = MBProgressHUDModeCustomView;
    [hud show:YES];
}



+ (UIWindow *)hudShowWindow {
    UIWindow *showWindow = nil;
    NSArray *windows = [[UIApplication sharedApplication] windows];
    if ([windows count] >= 2) {
        showWindow = [windows objectAtIndex:1];
    }
    else {
        showWindow = [[UIApplication sharedApplication] keyWindow];
    }
    return showWindow;
}

@end
